// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: Aquis Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// ========================
// Aliases
// ========================

private alias object.value0 : object.timer
private alias object.value1 : object.startPos.x
private alias object.value2 : object.startPos.y
private alias object.value3 : object.turnCount
private alias object.value4 : object.wingFrame
private alias object.value5 : object.wingAnimTimer
private alias object.value6 : object.playerInRange

private alias 0 : AQUIS_SETUP
private alias 1 : AQUIS_MOVING
private alias 2 : AQUIS_SHOOT
private alias 3 : AQUIS_FLEE

// Player Aliases
private alias object.xpos : player.xpos
private alias object.ypos : player.ypos
private alias object.controlMode : player.controlMode

private alias object.value40 :	player.hitboxLeft
private alias object.value38 :	player.hitboxTop
private alias object.value41 :	player.hitboxRight
private alias object.value39 :	player.hitboxBottom


// ========================
// Function Declarations
// ========================

reserve function Aquis_DebugDraw
reserve function Aquis_DebugSpawn


// ========================
// Function Definitions
// ========================

private function Aquis_DebugDraw
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
end function


private function Aquis_DebugSpawn
	CreateTempObject(TypeName[Aquis], 0, object.xpos, object.ypos)
	object[tempObjectPos].direction = object.direction
end function


// ========================
// Events
// ========================

event ObjectUpdate
	switch object.state
	case AQUIS_SETUP
		if object.priority != PRIORITY_XBOUNDS_DESTROY
			object.priority = PRIORITY_ACTIVE
		end if

		if player[0].controlMode > CONTROLMODE_NONE // ???
			object.timer = -16
			object.state = AQUIS_SHOOT
		end if
		break

	case AQUIS_MOVING
		temp1 = 0x7FFFFFFF
		temp2 = 0
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			temp0 = object.xpos
			temp0 -= player[currentPlayer].xpos
			Abs(temp0)
			if temp0 < temp1
				temp1 = temp0
				temp2 = currentPlayer
			end if
		next
		currentPlayer = temp2

		if object.xpos > player[currentPlayer].xpos
			object.direction = FLIP_NONE

			object.xvel -= 0x1000
			if object.xvel < -0x10000
				object.xvel = -0x10000
			end if
		else
			object.direction = FLIP_X

			object.xvel += 0x1000
			if object.xvel > 0x10000
				object.xvel = 0x10000
			end if
		end if
		
		if object.ypos > player[currentPlayer].ypos
			object.yvel -= 0x1000
			if object.yvel < -0x10000
				object.yvel = -0x10000
			end if
		else
			object.yvel += 0x1000
			if object.yvel > 0x10000
				object.yvel = 0x10000
			end if
		end if

		object.xpos += object.xvel
		object.ypos += object.yvel

		if object.playerInRange == false
			temp0 = player[currentPlayer].xpos
			temp0 -= object.xpos
			if object.direction == FLIP_NONE
				if temp0 > -0x400000
					if temp0 <= 0
						CreateTempObject(TypeName[Aquis Shot], 0, object.xpos, object.ypos)
						object[tempObjectPos].xpos -= 0x100000
						object[tempObjectPos].ypos -= 0xA0000
						object[tempObjectPos].xvel = -0x30000
						object[tempObjectPos].yvel = 0x20000
						object.playerInRange = true
					end if
				end if
			else
				if temp0 < 0x400000
					if temp0 >= 0
						CreateTempObject(TypeName[Aquis Shot], 0, object.xpos, object.ypos)
						object[tempObjectPos].xpos += 0x100000
						object[tempObjectPos].ypos -= 0xA0000
						object[tempObjectPos].xvel = 0x30000
						object[tempObjectPos].yvel = 0x20000
						object.playerInRange = true
					end if
				end if
			end if
		end if

		object.timer++
		if object.timer == 128
			object.timer = 0
			object.xvel = 0
			object.yvel = 0
			object.state = AQUIS_SHOOT
		end if
		break

	case AQUIS_SHOOT
		object.timer++
		if object.timer == 32
			object.timer = 0
			object.playerInRange = false
			object.turnCount++
			if object.turnCount == 4
				object.xvel = -0x20000
				object.yvel = 0
				object.state = AQUIS_FLEE
			else
				object.state = AQUIS_MOVING
			end if
		end if
		break

	case AQUIS_FLEE
		object.xpos += object.xvel
		object.ypos += object.yvel
		break

	end switch

	object.wingFrame = object.wingAnimTimer
	object.wingFrame >>= 2
	object.wingFrame += 3
	object.wingAnimTimer++
	object.wingAnimTimer &= 7

	if object.outOfBounds == true
		temp0 = object.xpos
		temp1 = object.ypos
		object.xpos = object.startPos.x
		object.ypos = object.startPos.y
		if object.outOfBounds == true
			if object.priority != PRIORITY_XBOUNDS_DESTROY
				object.priority = PRIORITY_BOUNDS
				object.turnCount = 0
				object.timer = 0
				object.animationTimer = 0
				object.xvel = 0
				object.yvel = 0
				object.state = AQUIS_SETUP
			end if
		else
			object.xpos = temp0
			object.ypos = temp1
		end if
	end if

	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_TOUCH, object.entityPos, -16, -12, 16, 12, currentPlayer, player[currentPlayer].hitboxLeft, player[currentPlayer].hitboxTop, player[currentPlayer].hitboxRight, player[currentPlayer].hitboxBottom)
		if checkResult == true
			CallFunction(Player_BadnikBreak)
		end if
	next
end event


event ObjectDraw
	DrawSpriteFX(object.frame, FX_FLIP, object.xpos, object.ypos)
	DrawSpriteFX(object.wingFrame, FX_FLIP, object.xpos, object.ypos)
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone07")
	if checkResult == true
		LoadSpriteSheet("OOZ/Objects.gif")
		SpriteFrame(-15, -24, 29, 47, 1, 1)
		SpriteFrame(-16, -23, 30, 46, 31, 1)
		SpriteFrame(-15, -23, 29, 46, 62, 1)
		SpriteFrame(3, -13, 11, 13, 108, 18)
		SpriteFrame(3, -9, 13, 12, 108, 32)
	else
		LoadSpriteSheet("OWZ/Objects2.gif")
		SpriteFrame(-15, -24, 30, 47, 1, 1)
		SpriteFrame(-16, -23, 30, 46, 31, 1)
		SpriteFrame(-15, -23, 29, 46, 62, 1)
		SpriteFrame(7, -3, 11, 13, 110, 18)
		SpriteFrame(7, -3, 11, 12, 110, 32)
	end if

	foreach (TypeName[Aquis], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].direction = object[arrayPos0].propertyValue
		object[arrayPos0].direction &= FLIP_X
		object[arrayPos0].startPos.x = object[arrayPos0].xpos
		object[arrayPos0].startPos.y = object[arrayPos0].ypos
	next

	SetTableValue(TypeName[Aquis], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(Aquis_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(Aquis_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event


// ========================
// Editor Events
// ========================

event RSDKEdit
	if editor.returnVariable == true
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			checkResult = object.propertyValue
			break
			
		case 0 // direction
			checkResult = object.propertyValue
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			break
			
		case 0 // direction
			object.propertyValue = editor.variableValue
			break
			
		end switch
	end if
end event


event RSDKDraw
	GetBit(object.direction, object.propertyValue, 0)
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
end event


event RSDKLoad
	CheckCurrentStageFolder("Zone07")
	if checkResult == true
		LoadSpriteSheet("OOZ/Objects.gif")
		SpriteFrame(-15, -24, 29, 47, 1, 1)
	else
		LoadSpriteSheet("OWZ/Objects2.gif")
		SpriteFrame(-15, -24, 30, 47, 1, 1)
	end if

	AddEditorVariable("direction")
	SetActiveVariable("direction")
	AddEnumVariable("Left", 0)
	AddEnumVariable("Right", 1)
end event
